"""
角色模型
"""
from enum import Enum
from typing import List, Optional
from pydantic import BaseModel, Field
from datetime import datetime
from bson import ObjectId
from app.models.base import PyObjectId

class RoleScope(str, Enum):
    SYSTEM = "system"   # 系统级角色
    PROJECT = "project" # 项目级角色

class RoleBase(BaseModel):
    name: str = Field(..., description="角色名称")
    code: str = Field(..., description="角色代码")
    scope: RoleScope = Field(..., description="角色作用域")
    description: Optional[str] = Field(None, description="角色描述")
    permissions: List[str] = Field(default_factory=list, description="权限列表")
    is_default: bool = Field(False, description="是否为该作用域的默认角色")
    is_system: bool = Field(False, description="是否为系统预置角色(不可删除)") # 保留用于标识不可删除的预置角色
    is_active: bool = Field(True, description="是否激活")

class RoleInDB(RoleBase):
    id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
    created_at: datetime = Field(default_factory=datetime.utcnow)
    updated_at: datetime = Field(default_factory=datetime.utcnow)

    class Config:
        populate_by_name = True
        arbitrary_types_allowed = True
        json_encoders = {ObjectId: str}

class Role(RoleInDB):
    """角色模型"""
    pass